package com.chj.design.sk;

public class Code04_Command {
	// 命令执行的接口
	public interface Command {

		public void exe();

	}

	// 具体实现的命令
	public static class MyCommand implements Command {
		private Receiver receiver;

		public MyCommand(Receiver receiver) {
			this.receiver = receiver;
		}

		public void exe() {
			receiver.action();
		}
	}

	// 被调用者（士兵）
	public static class Receiver {
		public void action() {
			System.out.println("command received!");
		}
	}

	// 调用者（将军）
	public static class Invoker {

		private Command command;

		public Invoker(Command command) {
			this.command = command;
		}

		public void action() {
			command.exe();
		}
	}

	public static void main(String[] args) {
		// 被调用者（士兵）
		Receiver receiver = new Receiver();
		// 具体实现的命令
		Command cmd = new MyCommand(receiver);
		// 调用者（将军）
		Invoker invoker = new Invoker(cmd);
		invoker.action();
	}

}
